package com.core.zyyz.pcmc.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.google.common.base.CaseFormat;
import com.core.common.utils.StringUtils;
import com.core.zyyz.enums.BooleanEnum;
import com.core.zyyz.pcmc.domain.PcmcDataColumn;
import com.core.zyyz.pcmc.domain.PcmcDept;
import com.core.zyyz.pcmc.mapper.PcmcDataColumnMapper;
import com.core.zyyz.pcmc.mapper.PcmcDeptMapper;
import com.core.zyyz.pcmc.service.PcmcDeptService;
import com.core.zyyz.util.BooleanUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 机构Service业务层处理
 *
 * @author core
 * @date 2024-10-17
 */
@Service
public class PcmcDeptServiceImpl implements PcmcDeptService
{
    @Autowired
    private PcmcDeptMapper pcmcDeptMapper;
    @Autowired
    PcmcDataColumnMapper pcmcDataColumnMapper;

    /**
     * 查询机构
     *
     * @param deptCode 机构主键
     * @return 机构
     */
    @Override
    public PcmcDept selectPcmcDeptByDeptCode(String deptCode)
    {
        return pcmcDeptMapper.selectPcmcDeptByDeptCode(deptCode);
    }

    /**
     * 查询机构列表
     *
     * @param pcmcDept 机构
     * @return 机构
     */
    @Override
    public List<PcmcDept> selectPcmcDeptList(PcmcDept pcmcDept)
    {
        return pcmcDeptMapper.selectPcmcDeptList(pcmcDept);
    }

    /**
     * 新增机构
     *
     * @param pcmcDept 机构
     * @return 结果
     */
    @Override
    public int insertPcmcDept(PcmcDept pcmcDept)
    {
        return pcmcDeptMapper.insertPcmcDept(pcmcDept);
    }

    /**
     * 修改机构
     *
     * @param pcmcDept 机构
     * @return 结果
     */
    @Override
    public int updatePcmcDept(PcmcDept pcmcDept)
    {
        return pcmcDeptMapper.updatePcmcDept(pcmcDept);
    }

    /**
     * 批量删除机构
     *
     * @param deptCodes 需要删除的机构主键
     * @return 结果
     */
    @Override
    public int deletePcmcDeptByDeptCodes(String[] deptCodes)
    {
        return pcmcDeptMapper.deletePcmcDeptByDeptCodes(deptCodes);
    }

    /**
     * 删除机构信息
     *
     * @param deptCode 机构主键
     * @return 结果
     */
    @Override
    public int deletePcmcDeptByDeptCode(String deptCode)
    {
        return pcmcDeptMapper.deletePcmcDeptByDeptCode(deptCode);
    }


    /**
     * 查询机构附加属性
     * @param colSize
     * @param isInfo
     * @return
     */
    public List<Map<String, Map<String, Object>>> queryDeptAttribute(int colSize, String isInfo) {
           List<PcmcDataColumn> columnList = pcmcDataColumnMapper.selectTableCodeList("PCMC_DEPT");
           if (CollectionUtils.isEmpty(columnList)) {
                return Collections.emptyList();
            }
            List<PcmcDataColumn> attrColumnList = (List<PcmcDataColumn>)columnList.stream().filter(dataColumn -> (BooleanEnum.TRUE.is(dataColumn.getIsEnabled()) && StringUtils.startsWithIgnoreCase(dataColumn.getColumnCode(), "ATTRIBUTE"))).collect(Collectors.toList());
          if (CollectionUtils.isEmpty(attrColumnList)) {
             return Collections.emptyList();
        }
        return this.getControls(attrColumnList, colSize, isInfo);
    }

    public Map<String, String> queryDeptNameMap(List<String> deptCodeList) {
        List<PcmcDept> deptList = pcmcDeptMapper.queryDeptNameMap(deptCodeList.toArray(new String[0]));
        return CollectionUtils.isEmpty(deptList) ? null : (Map<String, String>)deptList.stream().collect(Collectors.toMap(PcmcDept::getDeptCode, PcmcDept::getDeptName));
    }


    public List<Map<String, Map<String, Object>>> getControls(List<PcmcDataColumn> columnList, int colSize, String isInfo) {
        List<Map<String, Map<String, Object>>> list = new ArrayList<>();
        for (int i = 0; i < columnList.size(); i++) {
            Map<String, Map<String, Object>> lineMap = null;
            if (i % colSize == 0) {
                lineMap = new LinkedHashMap<>();
                list.add(lineMap);
            } else {
                lineMap = list.get(list.size() - 1);
            }
            PcmcDataColumn dataColumn = columnList.get(i);
            Map<String, Object> controlMap = new HashMap<>();
            controlMap.put("span", Integer.valueOf(24 / colSize));
            controlMap.put("label", dataColumn.getColumnName());
            dataColumn.getDataType();
            controlMap.put("control", "text");
            if (BooleanUtil.isTrue(isInfo)) {
                controlMap.put("control", "string");
                controlMap.put("class", "hideBackground");
                controlMap.put("disabled", Boolean.valueOf(true));
            }
            List<Map<String, Object>> ruleList = new ArrayList<>();
            if (BooleanEnum.FALSE.is(dataColumn.getIsNull())) {
                Map<String, Object> ruleMap = new HashMap<>();
                ruleMap.put("required", Boolean.valueOf(true));
                ruleMap.put("message", "common_error_notEmpty");
                ruleList.add(ruleMap);
            }
            String columnValueSet = dataColumn.getEntityCode();
            if (StringUtils.isNotBlank(columnValueSet)) {
                controlMap.put("control", "remoteSelect");
                controlMap.put("valueField", "valueCode");
                controlMap.put("labelField", "valueName");
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("url", "/VALUE-SET/remoteselect/valuesetvalue");
                requestMap.put("method", "get");
                Map<String, Object> paramsMap = new HashMap<>();
                paramsMap.put("valueSetCode", columnValueSet);
                requestMap.put("params", paramsMap);
                controlMap.put("doRequest", requestMap);
            } else {
                Map<String, Object> ruleMap = new HashMap<>();
                ruleMap.put("max", Integer.valueOf(256));
                ruleMap.put("message", "common_error_length256");
                ruleList.add(ruleMap);
            }
            controlMap.put("rules", ruleList);
            lineMap.put(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, dataColumn.getColumnCode()), controlMap);
        }
        return list;
    }

}
